package com.atguigu.spring.tx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author caodan
 * @version 1.0
 * @date 2022/6/21 10:20
 */
@Component
public class Transaction {


	@Autowired
	private JdbcTemplate jdbcTemplate;


	@Autowired
	private Transaction transaction;

	@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
	public void save(int i){
		this.jdbcTemplate.execute("INSERT INTO family(`name`,age,sex) VALUES(1,1,1);");
		this.transaction.a();
		int j = 1 / i;
	}

	@Transactional(propagation = Propagation.REQUIRES_NEW)
	public void a() {
		this.jdbcTemplate.execute("INSERT INTO family(`name`,age,sex) VALUES(2,2,2);");
	}
}
